package cn.edu.zime.tjh47;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class Class5_2 extends AppCompatActivity implements View.OnClickListener {

    private static int data;
    private static int cnt;
    private static TextView tv_result, tv;
    private static Boolean flag = false;


    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.class5_b);
        Bundle bundle = getIntent().getExtras();
        data = bundle.getInt("key1");
        String s = bundle.getString("key2");
        tv_result = findViewById(R.id.tv_result);
        tv = findViewById(R.id.tv);
        tv.setText(s + "，欢迎您，您觉得当前的随机数是多少？");
        cnt = 0;
        findViewById(R.id.back).setOnClickListener(this);
        findViewById(R.id.guess).setOnClickListener(this);
    }

    private static void Guess(int a) {
        if (a > data) {
            ++cnt;
            tv_result.setText("猜大了，猜了" + cnt + "次");


        } else if (a < data) {
            ++cnt;
            tv_result.setText("猜小了，猜了" + cnt + "次");

        } else {
            ++cnt;
            tv_result.setText("猜对了，猜了" + cnt + "次");
            flag = true;
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.back) {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt("key2", cnt);
            bundle.putBoolean("key3", flag);
            intent.putExtras(bundle);
            setResult(RESULT_OK, intent);
            finish();
        } else if (view.getId() == R.id.guess) {

            int guess = 0;
            try {
                guess = Integer.parseInt(((EditText) findViewById(R.id.et_guess)).getText().toString());
            } catch (NumberFormatException e) {
                throw new RuntimeException(e);
            }
            Guess(guess);
        }
    }

    public void back(View view) {
        finish();
    }
}